<template>
  <van-icon
    :color="likeCount ? 'blue' : '#777'"
    :name="likeCount ? 'good-job' : 'good-job-o'"
    @click="setLikeCount"
  />
</template>

<script>
import { addLike, deleteLike } from "@/api/article";
export default {
  props: {
    id: {
      type: [String, Number],
      required: true,
    },
    likeCount: {
      type: [String, Number],
      required: true,
    },
  },
  methods: {
    async setLikeCount() {
      this.$toast.loading({
        duration: 0,
        message: "加载中...",
        forbidClick: true,
      });
      // 判断
      try {
        if (this.likeCount) {
          // 如果为true 取消点赞
          await deleteLike(this.id);
        } else {
          await addLike(this.id);
        }
        this.$emit("update:like-count", this.likeCount ? 0 : 1);
      } finally {
        this.$toast.clear();
      }
    },
  },
};
</script>

<style>
</style>